//package org.example.ecsp.application.service.impl;
//
//
//import cn.tedu.mivos.application.mapper.ApplicationMapper;
//import cn.tedu.mivos.application.pojo.dto.ApplicationQuery;
//import cn.tedu.mivos.application.pojo.dto.ApplicationSaveParam;
//import cn.tedu.mivos.application.pojo.entity.Application;
//import cn.tedu.mivos.application.pojo.vo.ApplicationVO;
//import cn.tedu.mivos.application.service.ApplicationService;
//import cn.tedu.mivos.audit.mapper.AuditMapper;
//import cn.tedu.mivos.audit.pojo.vo.AuditVO;
//import cn.tedu.mivos.audit.service.AuditService;
//import cn.tedu.mivos.enums.ApplicationStatusEnum;
//import cn.tedu.mivos.user.mapper.UserMapper;
//import cn.tedu.mivos.user.pojo.vo.UserVO;
//import cn.tedu.mivos.vehicle.mapper.VehicleMapper;
//import cn.tedu.mivos.vehicle.pojo.entity.Vehicle;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.StringJoiner;
//
//@Service
//@Slf4j
///*注解用于标记一个方法或类，使其在执行时自动开启数据库事务。如果方法执行成功，则提交事务；
//如果方法执行过程中抛出异常，则回滚事务。这确保了数据库操作的原子性和一致性。*/
//@Transactional
//public class ApplicationServiceImpl implements ApplicationService {
//    @Autowired
//    ApplicationMapper applicationMapper;
//    @Autowired
//    AuditService auditService;
//    @Autowired
//    UserMapper userMapper;
//    @Autowired
//    AuditMapper auditMapper;
//    @Autowired
//    VehicleMapper vehicleMapper;
//
//    @Override
//    public void save(ApplicationSaveParam applicationSaveParam) {
//        log.debug("开始处理新增申请单的请求，参数：{}", applicationSaveParam);
//        Application application = new Application();
//        BeanUtils.copyProperties(applicationSaveParam, application);
//        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
//        application.setCreateTime(new Date());
//        applicationMapper.insert(application);
//        //需要为此申请单生成对应的审批单数据
//        auditService.insertAudit(application);
//    }
//
//    @Override
//    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
//        log.debug("开始处理查询申请单的请求，参数：{}", applicationQuery);
//        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
//        //上面的查询结果中,没有包含审批人信息(前端中不显示审批人信息)
//        for (int i = 0; i < list.size(); i++){
//            ApplicationVO applicationVO = list.get(i);
//            //处理当前遍历到的申请单,给他补充审批人数据
//            assignAuditUserList(applicationVO);
//        }
//        return list;
//    }
//
//
//    private void assignAuditUserList(ApplicationVO applicationVO) {
//        //1.先准备空集合,用来保存申请单对应的审批人的姓名和id
//        ArrayList<String> auditUsernameList = new ArrayList<>();
//        ArrayList<Long> auditUserIdList = new ArrayList<>();
//        //2.根据传进来的申请单的数据,获取当前申请单的id,并根据此id查询出当前申请单对应的审批单数据
//        //注意:这里的数据可能是多条
//        List<AuditVO> auditVOList = auditService.selectAuditByApplicationId(applicationVO.getId());
//        //3.遍历approveVOList,为每一个approveVO对象
//        for (int i = 0; i < auditVOList.size(); i++){
//            AuditVO auditVO = auditVOList.get(i);
//            //4.取出当前的审批单中的审批人id
//            Long id = auditVO.getAuditUserId();
//            auditUserIdList.add(id);//将id添加到approveUserIdList中
//            //5.再根据这个审批人id,查询出当前审批人姓名(走userMapper)
//            UserVO userVO =userMapper.selectById(id);
//            auditUsernameList.add(userVO.getUsername());//将审批人的姓名添加到approveUsernameList中
//        }
//        //6.准备一个拼接工具,将所有的审批人的姓名连成一个字符串
//        StringJoiner stringJoiner = new StringJoiner(",");
//        for (String username : auditUsernameList) {
//            stringJoiner.add(username);
//        }
//        //将拼接好的姓名字符串赋值传入给申请单对象(ApplicationVO)的String auditUsernameList属性中,
//        // 形成如"tom,lily"
//        applicationVO.setAuditUsernameList(stringJoiner.toString());
//        //将approveUserIdList赋值给申请单对象(ApplicationVO)的List<Long> auditUserIdList属性中,
//        //结果是如:[106,103]
//        applicationVO.setAuditUserIdList(auditUserIdList);
//    }
//
//    @Override
//    public void cancel(Long id) {
//        log.debug("开始处理撤销申请单的请求的业务，参数：{}", id);
//        Application application = new Application();
//        application.setId(id);
//        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
//        application.setUpdateTime(new Date());
//        applicationMapper.update(application);
//        auditMapper.deleteAuditByApplicationId(id);
//    }
//
//    @Override
//    public void distribute(Long applicationId, Long vehicleId) {
//        log.debug("开始处理分配车辆业务，参数：{}", applicationId);
//        Application application = new Application();
//        application.setId(applicationId);
//        application.setVehicleId(vehicleId);
//        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
//        application.setUpdateTime(new Date());
//        applicationMapper.update(application);
//        Vehicle vehicle=new Vehicle();
//        vehicle.setId(vehicleId);
//        vehicle.setStatus("2");
//        vehicle.setUpdateTime(new Date());
//        vehicleMapper.update(vehicle);
//    }
//
//    @Override
//    public void back(Long applicationId, Long vehicleId) {
//        log.debug("开始处理还车申请单的业务，参数：{},{}", applicationId, vehicleId);
//        Application application = new Application();
//        application.setId(applicationId);
//        application.setStatus(ApplicationStatusEnum.END.getCode());
//        application.setVehicleId(null);
//        application.setUpdateTime(new Date());
//        applicationMapper.back(application);
//        Vehicle vehicle=new Vehicle();
//        vehicle.setId(vehicleId);
//        vehicle.setStatus("1");
//        vehicle.setUpdateTime(new Date());
//        vehicleMapper.update(vehicle);
//    }
//
//}
